﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using OA.BLL;

using OA.Model;
using OA.Model.DB;
using OA.Web.Areas.Member.Controllers;
using OA.Web.Attribute;
using OA.Web.Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;
using Z.EntityFramework.Plus;

namespace OA.Web.Areas.Management.Controllers
{
    [Area("Management")]
    public class RelatedCompanyController : BaseMemberController
    {

        private IMemoryCache _cache;
        public RelatedCompanyController(ICookie cookie, IHttpContextAccessor accessor, IMemoryCache cache) : base(accessor, cookie, cache)
        {
            _cache = cache;
        }

        [MyActionFilter("207001", "往来单位", "单位与产品", true, "iconzhishi")]
        public IActionResult Index()
        {
            return View();
        }

        [MyActionFilter("207001", "往来单位")]
        public IActionResult GetData(string keyword, int page = 1, int limit = 20)
        {
            Expression<Func<RelatedCompany, bool>> where = m => true;
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                where = m => (m.Name.Contains(keyword));
            }
            PageList<RelatedCompany> list = this.GetPageList<RelatedCompany>(where, page, limit);
            var result = new
            {
                code = "0",
                msg = "",
                count = list.RowCount,
                data = list.List
            };
            return Json(result);
        }

        [MyActionFilter("207001", "往来单位", "单位与产品", true, "iconzhishi")]
        public IActionResult ShowContact(int id)
        {
            ViewBag.ID = id;
            return View();
        }

        [MyActionFilter("207001", "往来单位")]
        public IActionResult GetContactData(int id, int page = 1, int limit = 20)
        {
            Expression<Func<RelatedCompanyContacts, bool>> where = m => m.RelatedCompanyID == id;

            PageList<RelatedCompanyContacts> list = this.GetPageList<RelatedCompanyContacts>(where, page, limit);
            var result = new
            {
                code = "0",
                msg = "",
                count = list.RowCount,
                data = list.List
            };
            return Json(result);
        }
        


        [HttpGet]
        [MyActionFilter("207001001", "添加往来单位", "单位与产品", false, "")]
        public IActionResult Create()
        {
            return View(new RelatedCompany());
        }




        [HttpPost]
        [MyActionFilter("207001001", "添加往来单位", "单位与产品", false, isUpdateDataBase: true)]
        public IActionResult Create(RelatedCompany model)
        {
            BaseResult baseResult = new BaseResult();
            model.AddEmpID = base.EmpID;
            if (!ModelState.IsValid)
            {
                baseResult.State = 400;
                baseResult.Msg = this.GetModelStateError(ModelState);                 
                return Json(baseResult);
            }
            else
            {
                this.AddInfo<RelatedCompany>(model);
                baseResult.Msg = "往来单位添加成功";
                return Json(baseResult);

            }

        }

        [HttpGet]
        [MyActionFilter("207001002", "编辑往来单位", "单位与产品", false, "")]
        public IActionResult Edit(int id)
        {
            RelatedCompany infoRelatedCompany = this.GetInfo<RelatedCompany>(id);
            if (infoRelatedCompany == null)
            {
                return Redirect("/Admin/Home/ShowInfo?msg=" + System.Web.HttpUtility.UrlEncode("沒有找到该往来单位", Encoding.UTF8));
            }
            return View(infoRelatedCompany);
        }

        [HttpPost]
        [MyActionFilter("207001002", "编辑往来单位", "单位与产品", false, isUpdateDataBase: true)]
        public IActionResult Edit(int id, RelatedCompany model)
        {
            BaseResult baseResult = new BaseResult();

            if (!ModelState.IsValid)
            {
                baseResult.State = 400;
                baseResult.Msg = this.GetModelStateError(ModelState);
                return Json(baseResult);
            }
            RelatedCompany infoPageGroup = this.GetInfo<RelatedCompany>(id);
            if (infoPageGroup == null)
            {
                baseResult.State = 400;
                baseResult.Msg = "沒有找到该分组";
                return Json(baseResult);
                 
            }
            this.Update<RelatedCompany>(infoPageGroup, model, new string[] { "Name", "Sort" });
            baseResult.Msg = "往来单位修改成功";
            return Json(baseResult);
        }

        [HttpGet]
        [MyActionFilter("207001002", "编辑往来单位", "单位与产品", false, isUpdateDataBase: true)]
        public IActionResult SetIsSupplier(int id,bool status)
        {
            BaseResult baseResult = new BaseResult();
            this.dbContextFactory.DbContext.RelatedCompany.Where(m => m.ID == id).Update(mbox => new RelatedCompany() { IsSupplier = status });
            return Json(baseResult);
        }


        [HttpGet]
        [MyActionFilter("207001002", "编辑往来单位", "单位与产品", false, isUpdateDataBase: true)]
        public IActionResult SetIsCustomer(int id, bool status)
        {
            BaseResult baseResult = new BaseResult();
            this.dbContextFactory.DbContext.RelatedCompany.Where(m => m.ID == id).Update(mbox => new RelatedCompany() { IsCustomer = status });
            return Json(baseResult);
        }



        [HttpPost]
        [MyActionFilter("207001003", "删除往来单位", "单位与产品", false, isUpdateDataBase: true)]
        public IActionResult Delete(long id)
        {
            Expression<Func<RelatedCompany, bool>> where = m => m.ID == id;
            this.Delete<RelatedCompany>(where);
            return Json(new { State = 200, Msg = "" });
        }

        [HttpGet]
        [MyActionFilter("207001003", "删除往来单位", "单位与产品",false, "",isUpdateDataBase:true)]
        public IActionResult DeleteContact(int cid,int id)
        {
            Expression<Func<RelatedCompanyContacts, bool>> where = m => m.ID == id && m.RelatedCompanyID == cid;
            this.Delete<RelatedCompanyContacts>(where);
            return Json(new { State = 200, Msg = "联系人删除成功!" });
        }

        [HttpGet]
        [MyActionFilter("207001001", "添加往来单位", "单位与产品", false, "")]
        public IActionResult AddContact(int relatedCompanyID)
        {
            return View(new RelatedCompanyContacts() { RelatedCompanyID = relatedCompanyID }) ;
        }




        [HttpPost]
        [MyActionFilter("207001001", "添加往来单位", "单位与产品", false,isUpdateDataBase:true)]
        public IActionResult AddContact(RelatedCompanyContacts model)
        {
            model.CreateDateTime = DateTime.Now;
            BaseResult baseResult = new BaseResult();
            if (!ModelState.IsValid)
            {
                baseResult.State = 400;
                baseResult.Msg = this.GetModelStateError(ModelState);
                return Json(baseResult);
            }
            else
            {
                this.AddInfo<RelatedCompanyContacts>(model);
                baseResult.Msg = "联系人添加成功";
                return Json(baseResult);

            }

        }

        [HttpGet]
        [MyActionFilter("207001002", "编辑往来单位", "单位与产品", false, "")]
        public IActionResult EditContact(int id, int relatedCompanyID)
        {
            RelatedCompanyContacts relatedCompanyContacts = this.GetInfo<RelatedCompanyContacts>(id);
            if (relatedCompanyContacts == null|| relatedCompanyContacts.RelatedCompanyID!= relatedCompanyID)
            {
                return Redirect("/Admin/Home/ShowInfo?msg=" + System.Web.HttpUtility.UrlEncode("沒有找到该联系人信息", Encoding.UTF8));
            }
            return View(relatedCompanyContacts);
        }

        [HttpPost]
        [MyActionFilter("207001002", "编辑往来单位", "单位与产品", false, isUpdateDataBase: true)]
        public IActionResult EditContact(int id, int relatedCompanyID, RelatedCompanyContacts model)
        {
            BaseResult baseResult = new BaseResult();

            if (!ModelState.IsValid)
            {
                baseResult.State = 400;
                baseResult.Msg = this.GetModelStateError(ModelState);
                return Json(baseResult);
            }
            RelatedCompanyContacts relatedCompanyContacts = this.GetInfo<RelatedCompanyContacts>(id);
            if (relatedCompanyContacts == null || relatedCompanyContacts.RelatedCompanyID != relatedCompanyID)
            {
                baseResult.State = 400;
                baseResult.Msg = "沒有找到该联系人信息";
                return Json(baseResult);
            }
            this.dbContextFactory.DbContext.RelatedCompanyContacts.Where(m => m.ID == id).Update(m=>new RelatedCompanyContacts() { Contacts= model.Contacts,Position=model.Position,Salutation=model.Salutation,Tel= model.Tel});
            baseResult.Msg = "联系人信息修改成功";
            return Json(baseResult);
        }


    }
}
